/**
 * 
 */
package com.snack.mgmt.ext;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.web.servlet.View;

/**
 * @author kandyxu
 * 
 */

@SuppressWarnings("unchecked")
public class JsonView implements View {
	private static final String CAHCE_CONTROL_NOCAHCHE = "no-cache";
	private static final String HEADER_PARAMA = "Parama";
	private static final String HEADER_CACHE_CONTROL = "Cache-Control";
	private static final String CONTENT_TYPE_JSON = "application/json";
	private static final String CONTENT_TYPE_TEXT = "text/html";

	private JsonConfig jsonConfig;

	private String characterEncoding;

	/**
	 * @param exceptionMessageAttribute
	 *            the exceptionMessageAttribute to set
	 */

	private String contentType;

	public JsonView() {
		contentType = CONTENT_TYPE_JSON;
		jsonConfig = new JsonConfig();

	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getCharacterEncoding() {
		return characterEncoding;
	}

	public void setCharacterEncoding(String characterEncoding) {
		this.characterEncoding = characterEncoding;
	}

	/**
	 * @return the jsonConfig
	 */
	public JsonConfig getJsonConfig() {
		return this.jsonConfig;
	}

	/**
	 * @param jsonConfig
	 *            the jsonConfig to set
	 */
	public void setJsonConfig(JsonConfig jsonConfig) {
		this.jsonConfig = jsonConfig;
	}

	public static void main(String[] args) {
		Map mp = new HashMap();
		java.util.Date d = new java.util.Date(System.currentTimeMillis());

		// mp.put("customer", customer);
		JsonConfig cfg = new JsonConfig();

		mp.put("date", new java.util.Date(System.currentTimeMillis()));
		System.out.println(JSONObject.fromObject(mp, cfg));

		System.out.println(JSONObject.fromObject("{\"date\":\"2008-12-26\"}",
				cfg));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.View#render(java.util.Map,
	 * javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void render(Map map, HttpServletRequest req, HttpServletResponse resp)
			throws Exception {

		resp.setContentType(CONTENT_TYPE_TEXT);
		if (this.characterEncoding != null
				&& !"".equals(this.characterEncoding)) {
			resp.setCharacterEncoding(this.characterEncoding);
		}
		resp.setHeader(HEADER_CACHE_CONTROL, CAHCE_CONTROL_NOCAHCHE);
		resp.setHeader(HEADER_PARAMA, CAHCE_CONTROL_NOCAHCHE);
		map.put("success", "success");
		try {
			JSON jsonObj = JSONObject.fromObject(map, jsonConfig);
			if (jsonObj != null) {
				Writer out = resp.getWriter();
				out.write(jsonObj == null ? "{}" : jsonObj.toString());
				out.flush();
				resp.flushBuffer();
			}
		} catch (IOException ex) {

		}
	}

}
